package config.controller;

import com.alibaba.fastjson.JSON;
import com.init.ConfigInitial;
import com.init.SingletonCache;
import com.util.LogUtil;
import com.util.PagedRequest;
import com.util.PagedResult;
import config.model.MailConfig;
import config.model.SmsConfig;
import config.service.ConfigService;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import sys.model.Questionnaire;
import sys.model.User;
import sys.service.QuestionnaireService;
import sys.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 问卷推送配置的 一系列配置
 *
 * @author shugen
 * @see
 */
@Controller
@RequestMapping("/Config")
public class ConfigController {
    @Autowired
    private ConfigService configService;
    @Autowired
    private QuestionnaireService qService;






    /*********************************************************
     * 函数名: getQuestionnaireView
     * 功能     : 获取问卷推送配置
     * 作者      ：g.huangshg 2017-2-07
     * 参数表: @param response
     * 参数表: @param request
     * 参数表: @param pagedRequest
     * 参数表: @param title
     * 参数表: @param start
     * 参数表: @param end
     * 参数表: @throws Exception
     * 返回值: String
     * 修改记录：
     *          日期                 修改人                 修改说明
     ********************************************************/
    @RequestMapping(value = "/getQuestionnaireConfig", method = { RequestMethod.POST,
            RequestMethod.GET },produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String getQuestionnaireConfig(HttpServletRequest request, HttpServletResponse response, PagedRequest pagedRequest, String title, String start, String end) throws Exception  {
        LogUtil.info("问卷推送配置====>");
        LogUtil.info("分页参数:"+	pagedRequest.getPage());
        LogUtil.info("分页页数:"+	pagedRequest.getRows());
        LogUtil.info("排序序名:"+	pagedRequest.getSidx());
        LogUtil.info("排序方式:"+	pagedRequest.getSord());
        LogUtil.info("问卷标题:"+	title);
        LogUtil.info("创建时间:"+	start+" ~ "+end);
        Map map =new HashMap();
        User user= UserUtil.getUser(request);
        if(user!=null){
            map.put("userid",user.getId());
        }
        map.put("page",pagedRequest.getPage());
        map.put("rows",pagedRequest.getRows());
        map.put("sidx",pagedRequest.getSidx());
        map.put("sord",pagedRequest.getSord());
        map.put("title",title);
        map.put("start",start);
        map.put("end",end);
        PagedResult<Questionnaire> r_list = qService.selectView(map);
        System.out.println(JSON.toJSONString(r_list));
        return JSON.toJSONString(r_list);
    }

    /*********************************************************
     * 函数名: updateQuestionnaire
     * 功能     : 修改问卷配置
     * 作者      ：g.huangshg 2017-3-01
     * 参数表: @param response
     * 参数表: @param request
     * 参数表: @param questionnaire
     * 参数表: @throws Exception
     * 返回值: String
     * 修改记录：
     *          日期                 修改人                 修改说明
     ********************************************************/
    @RequestMapping(value = "/updateQuestionnaireConfig", method = { RequestMethod.POST,
            RequestMethod.GET },produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String updateQuestionnaireConfig(Questionnaire q) throws Exception  {
        LogUtil.info("修改问卷推送配置====>");
        if(q!=null) {
            LogUtil.info("修改-->"+q.getTitle());
        }
        JSONObject json = new JSONObject();
        int i=qService.updateQuestionnaireConfig(q);
            if(i>0){
                json.put("status","success");
                json.put("msg","操作成功");
                List<Questionnaire> list;
                try {

                    list = (List<Questionnaire>) SingletonCache.getCacheElement(ConfigInitial.QuestionnareList, SingletonCache.getCache(ConfigInitial.Init));
                    if(list!=null&&!list.isEmpty()){
                        for (Questionnaire qq: list) {
                           if(qq.getId()==q.getId()){
                               if(q.getTitle().equals("pagestart"))
                                   qq.setPagestart(q.isPagestart());
                               else if(q.getTitle().equals("mailstart"))
                               qq.setMailstart(q.isMailstart());
                               else if(q.getTitle().equals("wechatstart"))
                               qq.setWechatstart(q.isWechatstart());
                               else if(q.getTitle().equals("start"))
                               qq.setStart(q.getStart());
                               break;
                           }
                        }
                    }
                }
                catch (Exception e){
                    LogUtil.info("重建问卷缓存====>");
                     list=new ArrayList<>();
                }
            }
            else{
                json.put("status","false");
                json.put("msg","操作失败");
            }
        return json.toString();
    }
    @RequestMapping(value = "/selectMail", method = {RequestMethod.POST,
            RequestMethod.GET})
    @ResponseBody
    public String selectMail(HttpServletRequest request, HttpServletResponse response, PagedRequest pagedRequest) throws Exception  {
        LogUtil.info("邮箱配置查询====>");
        LogUtil.info("分页参数:"+	pagedRequest.getPage());
        LogUtil.info("分页页数:"+	pagedRequest.getRows());
        LogUtil.info("排序序名:"+	pagedRequest.getSidx());
        LogUtil.info("排序方式:"+	pagedRequest.getSord());
        Map map =new HashMap();
        map.put("page",pagedRequest.getPage());
        map.put("rows",pagedRequest.getRows());
        map.put("sidx",pagedRequest.getSidx());
        map.put("sord",pagedRequest.getSord());
        PagedResult<MailConfig> list = configService.selectMail(map);
        System.out.println(JSON.toJSONString(list));
        return JSON.toJSONString(list);
    }
    @RequestMapping(value = "/selectSms", method = {RequestMethod.POST,
            RequestMethod.GET})
    @ResponseBody
    public String selectSms(HttpServletRequest request, HttpServletResponse response, PagedRequest pagedRequest) throws Exception  {
        LogUtil.info("短信配置查询====>");
        LogUtil.info("分页参数:"+	pagedRequest.getPage());
        LogUtil.info("分页页数:"+	pagedRequest.getRows());
        LogUtil.info("排序序名:"+	pagedRequest.getSidx());
        LogUtil.info("排序方式:"+	pagedRequest.getSord());
        Map map =new HashMap();
        map.put("page",pagedRequest.getPage());
        map.put("rows",pagedRequest.getRows());
        map.put("sidx",pagedRequest.getSidx());
        map.put("sord",pagedRequest.getSord());
        PagedResult<SmsConfig> list = configService.selectSms(map);
        System.out.println(JSON.toJSONString(list));
        return JSON.toJSONString(list);
    }
    @RequestMapping(value = "/updateMail", method = {RequestMethod.POST,
            RequestMethod.GET},produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String updateMail(HttpServletRequest request, HttpServletResponse
            response,MailConfig mailConfig) throws IOException, JSONException {
        LogUtil.info("邮件配置修改====>");
        JSONObject json = new JSONObject();
        int i=configService.updateMail(mailConfig);
        if(i>0){
            json.put("status","success");
            json.put("msg","成功");
        }
        else{
            json.put("status","false");
            json.put("msg","失败");
        }
        return json.toString();

    }
    @RequestMapping(value = "/updateSms", method = {RequestMethod.POST,
            RequestMethod.GET},produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String updateSms(HttpServletRequest request, HttpServletResponse
            response,SmsConfig smsConfig) throws IOException, JSONException {
        LogUtil.info("短信配置修改====>");
        JSONObject json = new JSONObject();
        int i=configService.updateSms(smsConfig);
        if(i>0){
            json.put("status","success");
            json.put("msg","成功");
        }
        else{
            json.put("status","false");
            json.put("msg","失败");
        }
        return json.toString();


    }
}